﻿#ifndef MODBUSRTUCONTROLLER_H
#define MODBUSRTUCONTROLLER_H

#include <QObject>
#include "Controller/SerialController.h"
struct RegInfo
{
    uint16_t RegAddr;
    uint16_t RegValue;
};

class ModbusRtuController : public QObject
{
    Q_OBJECT
public:
    explicit ModbusRtuController(QObject *parent = nullptr);
    ~ModbusRtuController();

    enum MODBUSWORD{
        DOR01 = 0x01,
        DIR02 = 0x02,
        AOR03 = 0x03,
        AIR04 = 0x04,
        DOW05 = 0x05,
        AOR06 = 0x06,
        DOWM15 = 0x0F,
        AOWM16 = 0x10
    };

    const static uint8_t word_SingleRead = 0x03;
    const static uint8_t word_SingleWrite = 0x06;
    const static uint8_t word_MultiRead = 0x03;
    const static uint8_t word_MultiWrite = 0x10;

    bool modbus_02SingleRead(uint8_t node,uint16_t reg,bool& result);
    bool modbus_02MultiRead(uint8_t node,uint16_t reg,int16_t count, QVector<bool>& result);
    bool modbus_05SingleWrite(uint8_t node,uint16_t reg,const bool& value);
    bool modbus_SingleRead(uint8_t node,uint16_t reg,uint16_t count,QByteArray& recievedata);
    bool modbus_SingleWrite(uint8_t node,uint16_t reg,uint16_t value);
    bool modbus_MultiWrite(uint8_t node,uint16_t reg,QVector<uint16_t> values);

    bool ModbusUsart_Init(QString com,int band,int dataBits,int stopBits,int parity);

public slots:

signals:

private:
    SerialController *pUsart = nullptr;
};

class Modbus_WriteCmd
{
public:
    uint8_t Node;
    uint8_t KeyWord;
    uint16_t RegAddr;   //起始地址
    uint16_t Count; //寄存器数量，操作多寄存器时使用
    QVector<QVariant> RegsValue;//寄存器值

 //   QByteArray result;
    bool excute(ModbusRtuController* myModbus);
};
#endif // MODBUSRTUCONTROLLER_H
